package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxEmployeeList;
import com.adk.backend.service.EmployeeListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工列表管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/employee-list")
public class EmployeeListController {
    
    @Autowired
    private EmployeeListService employeeListService;
    
    /**
     * 分页查询员工列表
     */
    @RequiresPermission("list")
    @GetMapping("/list")
    public Result<PageResult<LxEmployeeList>> getEmployeeList(
            @RequestParam(required = false) String empName,
            @RequestParam(required = false) String empNo,
            @RequestParam(required = false) Integer deptId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("empName", empName);
        params.put("empNo", empNo);
        params.put("deptId", deptId);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        PageResult<LxEmployeeList> result = employeeListService.getEmployeeList(params);
        return Result.success(result);
    }
    
    /**
     * 获取所有员工（不分页）
     */
    @RequiresPermission("view")
    @GetMapping("/all")
    public Result<List<LxEmployeeList>> getAllEmployeeList(@RequestParam(required = false) Integer deptId) {
        List<LxEmployeeList> result = employeeListService.getAllEmployeeList(deptId);
        return Result.success(result);
    }
    
    /**
     * 根据部门ID获取员工列表
     */
    @RequiresPermission("view")
    @GetMapping("/by-dept")
    public Result<List<LxEmployeeList>> getEmployeeListByDeptId(@RequestParam Integer deptId) {
        List<LxEmployeeList> result = employeeListService.getEmployeeListByDeptId(deptId);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询员工
     */
    @RequiresPermission("view")
    @GetMapping("/detail")
    public Result<LxEmployeeList> getEmployeeDetail(@RequestParam Long id) {
        LxEmployeeList employee = employeeListService.getEmployeeById(id);
        if (employee != null) {
            return Result.success(employee);
        }
        return Result.error("员工不存在");
    }
    
    /**
     * 根据员工编号查询员工
     */
    @RequiresPermission("view")
    @GetMapping("/by-empno")
    public Result<LxEmployeeList> getEmployeeByEmpNo(@RequestParam String empNo) {
        LxEmployeeList employee = employeeListService.getEmployeeByEmpNo(empNo);
        if (employee != null) {
            return Result.success(employee);
        }
        return Result.error("员工不存在");
    }
    
    /**
     * 创建员工
     */
    @RequiresPermission("add")
    @PostMapping("/create")
    public Result<LxEmployeeList> createEmployee(@RequestBody LxEmployeeList employee) {
        try {
            // 检查员工编号是否已存在
            LxEmployeeList existing = employeeListService.getEmployeeByEmpNo(employee.getEmpNo());
            if (existing != null) {
                return Result.error("员工编号已存在");
            }
            
            LxEmployeeList result = employeeListService.createEmployee(employee);
            return Result.success("创建成功", result);
        } catch (Exception e) {
            log.error("创建员工失败", e);
            return Result.error("创建员工失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新员工
     */
    @RequiresPermission("edit")
    @PostMapping("/update")
    public Result<Boolean> updateEmployee(@RequestBody LxEmployeeList employee) {
        try {
            boolean success = employeeListService.updateEmployee(employee);
            if (success) {
                return Result.success("更新成功", true);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新员工失败", e);
            return Result.error("更新员工失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除员工
     */
    @RequiresPermission("del")
    @PostMapping("/delete")
    public Result<Boolean> deleteEmployee(@RequestParam Long id) {
        try {
            boolean success = employeeListService.deleteEmployee(id);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除员工失败", e);
            return Result.error("删除员工失败：" + e.getMessage());
        }
    }
}

